#include "client_works.h"

static Status doAdminMenu(int client_fd, const LoginMsg* lmsg);
static Status doNormalMenu(int client_fd, const LoginMsg* lmsg);

static Status doQuit(int client_fd);
static Status doLogin(int client_fd, LoginMsg* msg);
static Status doRegist(int client_fd);

static Status doAddStaff(int client_fd, StaffMsg msg);
static Status doDelStaff(int client_fd, StaffMsg msg);
static Status doUpdateInfo(int client_fd, StaffMsg msg);
static Status doSearch(int client_fd, StaffMsg msg);
static Status doHistory(int client_fd, const StaffMsg* msg);
static Status doCleanHistory(int client_fd, const StaffMsg* msg);

/* communicate with server */
/* return: quit client */
Status doBaseMenu(int client_fd) {
	int choice;
	LoginMsg lmsg;

	while (1) 
	{
		choice = -1;
		putchar('\n');
		PRINT_SPLITE;
		printf("1:注册 2:登陆 3:退出\n");
		PRINT_SPLITE;
		scanf("%d", &choice);
		while(getchar() != '\n');

		switch (choice)
		{
			case 1:
				if (doRegist(client_fd) == Success) {
					printf("注册成功！\n");
				}
				break;
			case 2:
				if (doLogin(client_fd, &lmsg) == Success)
				{
					if (lmsg.ID.a_type == Admin)
					{
						if (doAdminMenu(client_fd, &lmsg) == Success) {
							printf("返回上级菜单成功!\n");
						} else {
							return Success;
						}
					}
					else if (lmsg.ID.a_type == Normal) 
					{ 
						if (doNormalMenu(client_fd, &lmsg) == Success) {
							printf("返回上级菜单成功!\n");
						} else {
							return Success;
						}
					}
				}
				break;
			case 3:
				if (doQuit(client_fd) == Failure) {
					return Failure;
				}
				return Success;
				break;
			default:
				CHOOSE_ERR;
				break;
		} /* end of switch */
	} /* end of for(;;) */
	return Success;
}

static Status doAdminMenu(int client_fd, const LoginMsg* lmsg) {
	int choice;
	StaffMsg msg;
	/* record login id */
	msg.ID = lmsg->ID;

	while (1)
	{
		choice = -1;
		putchar('\n');
		PRINT_SPLITE;
		printf("1:添加 2:删除 3:更新 4:查询 5:返回 6:退出 7:历史 8:清除历史\n");
		PRINT_SPLITE;
		scanf("%d", &choice);
		while(getchar() != '\n');

		switch (choice)
		{
			case 1:
				if (doAddStaff(client_fd, msg) == Success) {
					printf("添加成功！\n");
				}
				break;
			case 2:
				if (doDelStaff(client_fd, msg) == Success) {
					printf("删除成功！\n");
				}
				break;
			case 3:
				if (doUpdateInfo(client_fd, msg) == Success) {
					printf("更新成功！\n");
				}
				break;
			case 4:
				doSearch(client_fd, msg);
				printf("查找结束\n");
				break;
			case 5:
				return Success;
				break;
			case 6:
				doQuit(client_fd);
				return Failure; 
				break;
			case 7:
				if (doHistory(client_fd, &msg) == Success) {
					printf("历史记录查找完毕！\n");
				}
				break;
			case 8:
				if (doCleanHistory(client_fd, &msg) == Success) {
					printf("历史记录清理完毕！\n");
				}
				break;
			default:
				CHOOSE_ERR;
				break;
		} /* end of switch */
	} /* end of for(;;) */
	return Success;
}

static Status doNormalMenu(int client_fd, const LoginMsg* lmsg) {
	int choice;
	StaffMsg msg;
	/* record login id */
	msg.ID = lmsg->ID;
	while (1)
	{
		choice = -1;
		putchar('\n');
		PRINT_SPLITE;
		printf("1:更新 2:查询 3:返回 4:退出 5:历史 6:清除历史");
		PRINT_SPLITE;
		scanf("%d", &choice);
		while(getchar() != '\n');

		switch (choice)
		{
			case 1:
				if (doUpdateInfo(client_fd, msg) == Success) {
					printf("更新成功！\n");
				}
				break;
			case 2:
				if (doSearch(client_fd, msg) == Success) {
					printf("查找结束\n");
				}
				break;
			case 3:
				return Success;
				break;
			case 4:
				doQuit(client_fd);
				return Failure; 
				break;
			case 5:
				if (doHistory(client_fd, &msg) == Success) {
					printf("历史记录查找完毕！\n");
				}
				break;
			case 6:
				if (doCleanHistory(client_fd, &msg) == Success) {
					printf("历史记录清理完毕！\n");
				}
				break;
			default:
				CHOOSE_ERR;
				break;
		} /* end of switch */
	} /* end of for(;;) */
	return Success;
}

static Status doQuit(int client_fd) {
	Status control = Quit;
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	return Success;
}

static Status doLogin(int client_fd, LoginMsg* msg) {
	/* fill in Request */
	Status control = Login;
	if (getLoginMsg(msg, L_Account|L_Passwd) == Failure) {
		return Failure;
	}
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	if (sendLogin(client_fd, msg) == Failure) {
		return Failure;
	}
	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("账户或者密码错误\n");
		return Failure;	
	}
	if (recvLogin(client_fd, msg) == Failure) {
		return Failure;
	}
	return Success;
}

static Status doRegist(int client_fd) {
	/* fill in Request */
	Status control = Regist;
	LoginMsg l_msg;
	StaffMsg s_msg;
	if (getLoginMsg(&l_msg, L_aType|L_Account|L_Passwd) == Failure) {
		return Failure;
	}
	if (getStaffMsg(&s_msg, S_All) == Failure) {
		return Failure;
	}
	/* associate account with u_id */
	l_msg.ID.u_id = s_msg.ID.u_id;
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	if (sendLogin(client_fd, &l_msg) == Failure) {
		return Failure;
	}
	if (sendStaff(client_fd, &s_msg) == Failure) {
		return Failure;
	}
	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("此id已被占用\n");
		return Failure;
	}
	return Success;
}

static Status doAddStaff(int client_fd, StaffMsg msg) {
	/* fill in Request */
	Status control = AddStaff;
	if (getStaffMsg(&msg, S_All) == Failure) {
		return Failure;
	}
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	if (sendStaff(client_fd, &msg) == Failure) {
		return Failure;
	}
	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("此用户已存在\n");
		return Failure;
	}
	return Success;
}

static Status doDelStaff(int client_fd, StaffMsg msg) {
	/* fill in Request */
	Status control = DelStaff;
	int flag = 0;
	chooseItems(&flag);
	if (flag == 0) {
		printf("未选择任何有效的列\n");
		return Failure;
	}
	msg.flag = flag;
	if (getStaffMsg(&msg, flag) == Failure) {
		return Failure;
	}
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	if (sendStaff(client_fd, &msg) == Failure) {
		return Failure;
	}
	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("删除目标不存在\n");
		return Failure;
	}
	return Success;
}

static Status doUpdateInfo(int client_fd, StaffMsg msg) {
	/* fill in Request */
	Status control = UpdateInfo;
	/* update by u_id */
	int flag = S_uID;
	chooseItems(&flag);
	if (msg.ID.a_type == Admin && flag == S_uID) {
		printf("未选择任何有效的列\n");
		return Failure;
	} else {
		msg.flag = flag;
	}
	if (msg.ID.a_type == Normal && flag == (S_uID|S_NorBlock)) {
		printf("未选择任何有效的列\n");
		return Failure;
	} else {
		msg.flag = flag & (~S_uID);
	}
	if (getStaffMsg(&msg, msg.flag) == Failure) {
		return Failure;
	}
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	if (sendStaff(client_fd, &msg) == Failure) {
		return Failure;
	}
	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("更新目标不存在\n");
		return Failure;
	}
	return Success;
}

static Status doSearch(int client_fd, StaffMsg msg) {
	/* fill in Request */
	Status control = SearchInfo;
	int flag = 0;
	if (msg.ID.a_type == Admin) {
		chooseItems(&flag);
		if (flag == 0) {
			printf("未选择任何有效的列\n");
			return Failure;
		} else if (flag == (S_All)) {
			msg.flag = -1;
		} else {
			msg.flag = flag;
			if (getStaffMsg(&msg, flag) == Failure) {
				return Failure;
			}
		}
	} else {
		msg.flag = S_uID;
	}
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	if (sendStaff(client_fd, &msg) == Failure) {
		return Failure;
	}

	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Continue) {
		/* printf header */
		while (recvStaff(client_fd, &msg) == Success) {
			if (msg.flag < 0) {
				break;
			}
			printf("%s, ", msg.name);
		}
		putchar('\n');
		while (recvStaff(client_fd, &msg) == Success) {
			if (msg.flag < 0) {
				break;
			}
			showStaffMsg(&msg, S_All);
		}
	}
	return Success;
}

static Status doHistory(int client_fd, const StaffMsg* msg) {
	/* fill in Request */
	Status control = History;
	HistoryMsg h_msg;
	h_msg.ID = msg->ID;
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* send msg */
	if (sendHistory(client_fd, &h_msg) == Failure) {
		return Failure;
	}
	
	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("历史记录为空\n");
		return Failure;
	}
	while (recvHistory(client_fd, &h_msg) == Success) {
		if (h_msg.flag < 0) {
			break;
		}
		showHistoryMsg(&h_msg);
	}
	return Success;
}

static Status doCleanHistory(int client_fd, const StaffMsg* msg) {
	/* fill in Request */
	Status control = CleanHistory;
	HistoryMsg h_msg;
	h_msg.ID = msg->ID; 
	/* send Request */
	if (sendStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* send msg */
	if (sendHistory(client_fd, &h_msg) == Failure) {
		return Failure;
	}

	/* recv reply*/
	if (recvStatus(client_fd, &control) == Failure) {
		return Failure;
	}
	/* handle reply */
	if (control == Stop) {
		printf("当前用户没有历史纪录\n");
		return Failure;
	}
	return Success;
}

